package chesskers;

import java.awt.Image;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import chess.SpecialMove;
import chesskers.ImagePanel;

public abstract class GamePiece implements java.io.Serializable{

	public int[][][] movePossibilities;
	public int[][][] attackPossibilities;
	
	//*Possibilities is a collection of paths.
	//Each path is a collection of int[2]'s which are the X and Y offsets of its move.
	//If another piece is found along the path, it stops "moving" along that path
	//For example, a knight has 8 paths of length 1
	//A rook has 4 paths of length 7 (ex, if it can only move up two squares, it will stop that path after 2 squares)
	
	public boolean isBlack;
	public boolean isUnmoved;
	
		
	public transient BufferedImage pic;
	
	protected int x;
	protected int y;
	
	public GamePiece(boolean black, int x_, int y_) {
		isBlack = black;
		isUnmoved = true;
	
		x = x_;
		y = y_;
		
		
		pic = null;
		try {
			pic = ImageIO.read(new File(getFilePath()));
		} catch (IOException e) {
		}
	}
	
	public abstract String getFilePath();
	
	public void loadImage(){
		try{
			pic = ImageIO.read(new File(getFilePath()));
		}
		catch(IOException e){
		}
	}
	
	public GamePiece[][] moveTo(int x_, int y_, GamePiece[][] board){
		isUnmoved = false;
		board[x][y] = null;
		x = x_;
		y = y_;
		board[x_][y_] = this;
		return board;
	}
	
	public abstract GamePiece[][] attackTo(int x_, int y_, GamePiece[][] board);
	
	public ArrayList<Point> getMovePossibilities(GamePiece[][] board){
		ArrayList<Point> validMoves = new ArrayList<Point>();
		for (int i = 0; i < movePossibilities.length; i++){
			for (int j = 0; j < movePossibilities[i].length; j++){
				try{
					if (board[x+movePossibilities[i][j][0]][y+movePossibilities[i][j][1]] == null) {
						validMoves.add(new Point(x+movePossibilities[i][j][0], y+movePossibilities[i][j][1]));
					}else{
						break;
					}
				}catch( ArrayIndexOutOfBoundsException e ) {
					break;
				}
			}
		}
		return validMoves;	
	}
	
	public abstract ArrayList<Point> getAttackPossibilities(GamePiece[][] board);

	public List<SpecialMove> getSpecialMovePossibilities() {
		return new ArrayList<SpecialMove>();
	}

	public abstract String getName();
	
}